package com.tenglong.teacher.ui.downApk.service;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.File;

/**
 * @author hdx on 2019/2/26.
 */
public class APKDownloadService extends Service {
    private DownloadBinder mBinder = new DownloadBinder();
    private ServiceHandler mServiceHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        mServiceHandler = new ServiceHandler(this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
        mServiceHandler.onDestroy();
    }

    public void load(String url, DownloadCallback callback) {
        mServiceHandler.downLoad(url,callback);
    }

    @Override
    public void onDestroy() {
        mServiceHandler.onDestroy();
        super.onDestroy();
    }

    public interface DownloadCallback {
        void onPrepare();

        void onProgress(int progress);

        void onComplete(File file);

        void onFail(String msg);

        Intent install(File file);
    }

    public class DownloadBinder extends Binder
    {
        public APKDownloadService getService(){
            return APKDownloadService.this;
        }
    }
}
